package com.zline.foodraise.adapter;

import android.content.Context;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.zline.foodraise.R;
import com.zline.foodraise.entity.TemplateBean;
import com.zline.foodraise.util.DeviceUtil;
import com.zline.foodraise.util.ImageUtil;
import com.zline.foodraise.view.TemplateChooseItem;
import com.zline.foodraise.view.TemplateChooseItem_;

import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.RootContext;

/**
 * Created by heshantao on 15/8/30.
 * 模板选择
 */
@EBean
public class TemplateChooseAdapter extends RecyclerViewBaseAdapter<TemplateBean, TemplateChooseItem> {
    private static final String LOG_TAG = TemplateChooseAdapter.class.getSimpleName();
    @RootContext
    Context cxt;
    private int itemWidth;
    private int horizentalNum = 2;
    private RelativeLayout.LayoutParams itemLayoutParams;
    DisplayImageOptions options;
    @Bean
    ImageUtil imageUtil;


    @Override
    protected TemplateChooseItem onCreateItemView(ViewGroup parent, int viewType) {
        TemplateChooseItem item = TemplateChooseItem_.build(cxt);
        setItemWidth();
        item.setLayoutParams(itemLayoutParams);
        return item;
    }


    /**
     * 设置每一个Item的宽高
     */
    public void setItemWidth() {
        int screenWidth = DeviceUtil.getDeviceWidth(cxt);
        int horizentalSpace = cxt.getResources().getDimensionPixelSize(R.dimen.template_rv_with_space);
        this.itemWidth = (screenWidth - horizentalSpace) / horizentalNum;
        this.itemLayoutParams = new RelativeLayout.LayoutParams(itemWidth, itemWidth * 3 / 2);
    }

    @Override
    public void onBindViewHolder(ViewWrapper<TemplateChooseItem> holder, int position) {
        TemplateChooseItem item = holder.getView();
        TemplateBean bean = arrys.get(position);
        if (options == null) {
            options = imageUtil.
                    getDisplayImageOptions(R.mipmap.default_template_thumb,
                            R.mipmap.default_template_thumb, R.mipmap.default_template_thumb);
        }
        item.bind(bean, position, options);
        item.setRecycleItemClickListener(itemClicked);
    }
}
